//引入express模块
const express = require('express')
//引入数据库连接模块
const c = require('../connection')
const { route } = require('./maintenance')
//创建路由器对象
const router = express.Router()

//配件信息模糊查询接口
/* router.get('/search', (req, res, next) => {
    var obj = req.query
    obj.pname == "undefined" ? "" : obj.pname
    obj.pcode == "undefined" ? "" : obj.pcode
    obj.aname == "undefined" ? "" : obj.aname
    console.log(obj.pname,obj.pcode,obj.aname);
    c.query(`select * from cs_coa,cs_partmessage where pcategory=aid and pname like ? and pcode like ? and aname like ?`,[`%${obj.pname}%`,`%${obj.pcode}%`,`%${obj.aname}%`],(err,r)=>{
        console.log(r);
        if(err){
            return next(err)
        }
        if(r.length>0){
            res.send({
                code:200,
                msg:'查询成功',
                data:r
            })
        }else{
            res.send({
                code:401,
                msg:'查询失败',
                data:r
            })
        }
    })
}) */
//  配件信息分页模糊查询接口
router.get('/search', (req, res, next) => {
    var obj = req.query
    pageNum = obj.pageNum * 1 || 1
    pageCount = 5
    start = (pageNum - 1) * pageCount
    obj.pname == "undefined" ? "" : obj.pname
    obj.pcode == "undefined" ? "" : obj.pcode
    obj.aname == "undefined" ? "" : obj.aname
    console.log(pageNum, pageCount, start, obj.pname, obj.pcode, obj.aname);
    c.query(`select count(pid) as count from cs_partmessage,cs_coa where pcategory=aid and pname like ? and pcode like ? and aname like ?`, [`%${obj.pname}%`, `%${obj.pcode}%`, `%${obj.aname}%`], (err, r) => {
        console.log(r);
        if (err) {
            return next(err)
        }
        if (r.length > 0) {
            var total = r[0].count
            c.query(`select * from cs_coa,cs_partmessage where pcategory=aid and pname like ? and pcode like ? and aname like ? limit ?,? `, [`%${obj.pname}%`, `%${obj.pcode}%`, `%${obj.aname}%`, start * 1, pageCount * 1], (err, r) => {
                if (err) {
                    return next(err)
                }
                if (r.length > 0) {
                    res.send({
                        code: 200,
                        data: r,
                        total: total,
                        pageCount: pageCount,
                        pageNum: pageNum
                    })
                } else {
                    res.send({
                        code: 401,
                        msg: '查询失败',
                    })
                }
            })
        }
    })
})


//配件新增接口
router.post('/add', (req, res, next) => {
    var obj = req.body
    c.query('insert into cs_partmessage set ?', [obj], (err, r) => {
        if (err) {
            return next(err)
        }
        console.log(r);
        res.send({
            code: 200,
            msg: '新增配件信息成功',
            data: r
        })
    })
})
//配件删除接口
router.post('/delete', (req, res) => {
    var obj = req.body
    console.log(obj);
    c.query(`delete from cs_partmessage where pid=?`, [obj.pid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.affectedRows === 1) {
            res.send({
                code: 200,
                msg: '删除成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '删除失败',
                data: r
            })
        }
    })
})
//配件修改接口
/* router.post('/update', (req, res) => {
    var obj = req.body
    console.log(obj);
    c.query(`update cs_partmessage set ? where pid=?`, [obj, obj.pid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.changedRows === 0) {
            res.send({
                code: 401,
                msg: '修改失败'
            })
        } else {
            res.send({
                code: 200,
                msg: '修改成功',
                data: r
            })
        }
    })
}) */
//配件信息查询接口
router.get('/', (req, res, next) => {
    var obj = req.query
    console.log(obj);
    c.query('select * from cs_partmessage where pid=?', [obj.pid], (err, r) => {
        if (err) {
            return next(err)
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '查询成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '查询失败'
            })
        }
    })
})
//配件信息修改接口
router.post('/update', (req, res) => {
    var obj = req.body
    console.log(obj);
    c.query(`update cs_partmessage set ? where pid=?`, [obj, obj.pid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.changedRows === 0) {
            res.send({
                code: 401,
                msg: '修改失败'
            })
        } else {
            res.send({
                code: 200,
                msg: '修改成功',
                data: r
            })
        }
    })
})
//配件入账修改出库时间接口
router.post('/upt', (req, res) => {
    var obj = req.body
    console.log(obj);
    c.query(`update cs_partmessage set bank=bank-?,utime=? where pcode=?`, [obj.bank, obj.utime, obj.pcode], (err, r) => {
        if (err) {
            throw err
        }
        if (r.changedRows > 0) {
            res.send({
                code: 200,
                msg: '修改成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '修改失败',
                data: r
            })
        }
    })
})
module.exports = router